export default {
    data(){
      return {
          localButtons:[]
      }
    },
    methods:{
        /**
         * 获取状态。
         * @param type
         * @param negation
         * @returns {boolean}
         */
        getStatus(type){
            let status="";
            let bpmInst=this.curVm.bpmInst;
            if(bpmInst && bpmInst.status){
                status=bpmInst.status;
            }
            let rtn= status== type;
            return rtn;
        },
        /**
         * 是否可以作废
         * @returns {boolean}
         */
        canCancel(){
            let status=this.curVm.bpmInst && this.curVm.bpmInst.status || "" ;
            let ary=["RUNNING","SUPSPEND"];
            if(ary.includes(status)){
                return true;
            }
            return false;
        },

        initButtons(){
            let self = this ;

            this.localButtons.length=0;

            let defaultBtn = [
                {
                    show:self.curVm.canTrack,
                    id:'btnTracked',
                    method:'doTracked',
                    style:false,
                    name:self.curVm.trackText,
                    defaultBtn:true
                },
                {
                    show:self.curVm.allowTransfer,
                    id:'btnTransfer',
                    method:'transferTo',
                    style:false,
                    name:self.curVm.i18n('list.forward','转发'),
                    defaultBtn:true
                },
                {
                    show:self.curVm.allowTransReply,
                    id:"btnTransReply",
                    method:'transferReply',
                    style:false,
                    name:self.curVm.i18n('list.replyFor','回复转发'),
                    defaultBtn:true
                },
                {
                    id:"btnBack",
                    method:'retrieveTaskNode',
                    style:false,
                    name:self.curVm.i18n('list.fetchTask','取回任务'),
                    show:self.curVm.canRevoke,
                    defaultBtn:true
                },
                {
                    show:self.curVm.bpmCommuBtnShow,
                    id:"btnCommuHistory",
                    method:'doCommuHistory',
                    style:false,
                    name:self.curVm.i18n('list.communicate','沟通情况'),
                    defaultBtn:true
                },
                {
                    show:self.curVm.showMsgBtn,
                    id:"btnMessage",
                    method:'showMsg',
                    style:false,
                    name:self.curVm.i18n('list.leaveMsg','留言'),
                    defaultBtn:true
                },
                {
                    id:"btnPrint",
                    method:'formPrint',
                    style:false,
                    name:self.curVm.$ti18('rxForm.print','打印'),
                    defaultBtn:true
                },
                {
                    show:self.getStatus('SUPSPEND'),
                    id:"restoreInst",
                    method:'restoreInst',
                    style:false,
                    name:self.curVm.i18n('list.restore','恢复'),
                    defaultBtn:false
                },
                {
                    id:"supspendInst",
                    method:'supspendInst',
                    style:false,
                    name:self.curVm.i18n('list.suspend','暂停'),
                    show:self.getStatus('RUNNING'),
                    defaultBtn:false
                },
                {
                    show:self.canCancel(),
                    id:"doDiscard",
                    method:'doDiscard',
                    style:false,
                    name:self.curVm.i18n('list.cancel','作废'),
                    defaultBtn:false
                }

            ]

            let btnData = [...defaultBtn.filter(item=>{
                return item.defaultBtn;
            })];

            //按钮进行合并。
            if(this.buttons.length > 0){
                btnData = this.buttons.map(item=>{
                    let hasBtn = defaultBtn.find(btn=>{
                        return btn.id == item.id
                    });

                    return hasBtn ? {...hasBtn,...item} : {...item};
                })
            }

            //对条件进行处理。
            btnData = btnData.filter(item=>{
                if(!item.hasOwnProperty('show')){
                    return true;
                }
                return  item.show;
            })

            console.info(JSON.stringify(btnData));



            //找到独立激活的按钮，先删除，再添加到末尾。
            let hasStyleIndex = btnData.findIndex(item=>{
                return item.style=="true" || item.style==true;
            });
            if(hasStyleIndex!=-1){
                let _data = btnData.splice(hasStyleIndex,1);
                btnData.push(_data[0]);
            }



            this.localButtons= btnData ;
        }
    },
    watch:{
      "curVm.bpmInst":{
           handler:function (val, oldVal) {
               this.initButtons();
           },
           deep:true

      }
    }
}